#include "manager.h"

#include <stdio.h>
#include <stdlib.h>

PointList ** dividePoints(PointList * input, int segments){
	long i, interval, remainder;
	int segmentIndex;
	PointList ** dividedPoints;

	interval = input->length / segments;
	remainder = input->length % segments;

	//printf("Interval=%ld\nRemainder=%ld\n", interval, remainder);
	
	//allocate memory
	dividedPoints = (PointList**)malloc(segments * sizeof(PointList*));
	for(i = 0; i < segments; i++){
		if(i == segments-1){
			dividedPoints[i] = newPointList(interval + remainder);
		}
		else
			dividedPoints[i] = newPointList(interval);
	}
	
	//populate segments with points
	for(i = 0; i < input->length; i++){
		segmentIndex = i/interval;
		//printf("Segment index=%d\n", segmentIndex);
		if(i >= segments * interval)
			segmentIndex = segments-1;
		
		addPoint(dividedPoints[segmentIndex], input->points[i]);
	}

	return dividedPoints;
}